home *** CD-ROM | disk | FTP | other *** search
- /*
- * hippoxdr.c - XDR routines for hippo I/O
- *
- * Copyright (C) 1991 The Board of Trustees of The Leland Stanford
- * Junior University. All Rights Reserved.
- *
- * $Id: hippoxdr.c,v 3.14 1992/04/07 19:02:44 rensing Rel $
- *
- * by Paul Rensing
- */
-
- #include <stdlib.h>
- #include <string.h>
- #include <limits.h>
- #include "hippo.h"
-
- #ifdef VM
- #include <manifest.h>
- #include "h_xdr.h"
- #include "h_util.h"
- #else
- #include "hippoxdr.h"
- #include "hippoutil.h"
- #endif
-
- /*
- * for machines which are naturally big-endian, IEEE floating point,
- * define EASY_XDR so that the ntuple data array (which is the vast majority
- * of a hippo file) is written out without checking for conversion.
- */
- #if defined(aix6000) || defined(NeXT) || defined(sun4) || defined(sgi)
- #define EASY_XDR
- #endif
-
- GLOB_QUAL const char hippoxdr_c_rcsid[] = "$Id: hippoxdr.c,v 3.14 1992/04/07 19:02:44 rensing Rel $";
-
- GLOB_QUAL const char hippostruct_h_rcsid[] = STRUCT_VERSION;
- #define CURRENT_VER_NUM 3.8
- /* version number of data being read/written */
- static float fileVersion = CURRENT_VER_NUM;
-
- static bool_t xdr_mywrapstring(XDR *xdrs, char **string);
-
-
- static bool_t xdr_mywrapstring(XDR *xdrs, char **string)
- {
- #ifndef __STDC__
- static
- #endif
- char blank[]=" ";
- char *pb = blank;
- int rc;
-
- if (xdrs->x_op == XDR_ENCODE && *string == NULL)
- rc = xdr_wrapstring(xdrs,&pb);
- else
- rc = xdr_wrapstring(xdrs,string);
-
- return rc;
- }
-
-
- bool_t xdr_floatarray(XDR *xdrs, caddr_t *objp, unsigned int *count,
- unsigned int max)
- {
- #ifdef EASY_XDR
- switch (xdrs->x_op)
- {
- case XDR_FREE:
- return xdr_array(xdrs, objp, count, max, sizeof(float), xdr_float);
- break;
-
- case XDR_DECODE: /* ie. read */
- if (! XDR_GETLONG(xdrs,(long *)count))
- return (FALSE);
-
- if ( *objp == NULL && *count != 0 &&
- (*objp = (char *) malloc(*count*sizeof(float))) == NULL)
- return FALSE;
-
- if (*count != 0)
- return XDR_GETBYTES(xdrs,*objp,
- (unsigned int) *count*sizeof(float));
- break;
-
- case XDR_ENCODE:
- if (! XDR_PUTLONG(xdrs,(long *)count))
- return (FALSE);
-
- if (*count > 0)
- return XDR_PUTBYTES(xdrs,*objp,*count*sizeof(float));
- break;
- }
- return 1;
- #else
- return xdr_array(xdrs, objp, count, max, sizeof(float), xdr_float);
- #endif
- }
-
- bool_t xdr_rectangle(XDR *xdrs, rectangle *objp)
- {
- if (!xdr_float(xdrs, &objp->origin.x)) {
- return (FALSE);
- }
- if (!xdr_float(xdrs, &objp->origin.y)) {
- return (FALSE);
- }
- if (!xdr_float(xdrs, &objp->size.width)) {
- return (FALSE);
- }
- if (!xdr_float(xdrs, &objp->size.height)) {
- return (FALSE);
- }
- return (TRUE);
- }
-
-
-
-
- bool_t xdr_func_id(XDR *xdrs, func_id *objp)
- {
- func_id *next=NULL;
- bool_t more_data;
-
- while (1)
- {
- more_data = (*objp != NULL);
- if (!xdr_bool(xdrs, &more_data))
- {
- return (FALSE);
- }
-
- if (!more_data) break;
-
- if (xdrs->x_op == XDR_FREE)
- next = &(*objp)->next;
-
- if (!xdr_reference(xdrs, (caddr_t *)objp,
- sizeof(func_id_t), xdr_func_id_t)) {
- return (FALSE);
- }
- objp = (xdrs->x_op == XDR_FREE) ?
- next : &(*objp)->next;
- }
- *objp = NULL;
-
- return(TRUE);
- }
-
- bool_t xdr_func_id_t(XDR *xdrs, func_id_t *objp)
- {
- char *tmp;
-
- /*
- * bug on RS/6000: &objp->name is not allowed because it is not
- * in memory
- */
- tmp = objp->name;
-
- if (!xdr_string(xdrs, &tmp, FUNCNAMELEN)) {
- return (FALSE);
- }
-
- /*
- * don't bother with the function pointer. Reconstruct it later
- * using the function name (objp->name).
- */
-
- if (!xdr_array(xdrs, (char **)&objp->paramBlk,
- (u_int *)&objp->blkSize, UINT_MAX,
- sizeof(double), xdr_double)) {
- return (FALSE);
- }
-
- if (!xdr_enum(xdrs, (int *)&objp->lineStyle)) {
- return (FALSE);
- }
-
- return (TRUE);
- }
-
-
- bool_t xdr_ntuple_t(XDR *xdrs, ntuple_t *objp)
- {
- unsigned int i;
-
- if (!xdr_int(xdrs, &objp->ndim))
- return (FALSE);
- if (!xdr_int(xdrs, &objp->rev))
- return (FALSE);
-
- if (xdrs->x_op == XDR_FREE)
- i = objp->memAlloc*objp->ndim;
- else
- i = objp->ndata*objp->ndim;
- if (!xdr_floatarray(xdrs, (char **)&objp->data, &i, UINT_MAX))
- return (FALSE);
-
-
- if (xdrs->x_op == XDR_DECODE)
- {
- objp->ndata = i/objp->ndim;
- objp->memAlloc = objp->ndata;
- }
-
- if (!xdr_int(xdrs, &objp->extremeBad)) {
- return (FALSE);
- }
- if (!xdr_floatarray(xdrs, (char **)&objp->nlow, (u_int *)&objp->ndim,
- UINT_MAX))
- return (FALSE);
- if (!xdr_floatarray(xdrs, (char **)&objp->nhigh, (u_int *)&objp->ndim,
- UINT_MAX))
- return (FALSE);
-
- if (!xdr_mywrapstring(xdrs, (char **)&objp->title )) {
- return (FALSE);
- }
-
- if (xdrs->x_op == XDR_DECODE)
- {
- if ( (objp->label = (char **)malloc(objp->ndim * sizeof(char *)))
- == NULL)
- {
- return (FALSE);
- }
- }
- for (i=0; i<objp->ndim; i++)
- {
- if (xdrs->x_op == XDR_DECODE) objp->label[i] = NULL;
- if (!xdr_mywrapstring(xdrs, (char **)&objp->label[i] )) {
- return (FALSE);
- }
- }
- if (xdrs->x_op == XDR_FREE)
- free(objp->label);
-
- return (TRUE);
- }
-
-
-
-
- bool_t xdr_ntuple(XDR *xdrs, ntuple *objp)
- {
- if (!xdr_pointer(xdrs, (char **)objp, sizeof(ntuple_t), xdr_ntuple_t))
- {
- return (FALSE);
- }
- return (TRUE);
- }
-
-
- bool_t xdr_bins_t(XDR *xdrs, bins_t *objp)
- {
- void *p;
- unsigned int c;
- int i,j;
- int oldTot[3][3]; /* for reading pre 3.8 version */
-
- p = (void *) &objp->flags;
- c = sizeof(objp->flags);
- if (!xdr_bytes(xdrs, &p, &c, c))
- {
- return (FALSE);
- }
-
- /*
- * xAxis data
- */
- if (!xdr_int(xdrs, &objp->xAxis.nBins)) {
- return (FALSE);
- }
- if (!xdr_float(xdrs, &objp->xAxis.low)) {
- return (FALSE);
- }
- if (!xdr_float(xdrs, &objp->xAxis.high)) {
- return (FALSE);
- }
- if (!xdr_vector(xdrs, (char *)objp->xAxis.moments, 3,
- sizeof(float), xdr_float))
- {
- return (FALSE);
- }
-
- /*
- * yAxis data
- */
- if (!xdr_int(xdrs, &objp->yAxis.nBins))
- return (FALSE);
- if (!xdr_float(xdrs, &objp->yAxis.low))
- return (FALSE);
- if (!xdr_float(xdrs, &objp->yAxis.high))
- return (FALSE);
- if (!xdr_vector(xdrs, (char *)objp->yAxis.moments, 3,
- sizeof(float), xdr_float))
- return (FALSE);
-
- if (!xdr_int(xdrs, &objp->binAlloc))
- return (FALSE);
- if (!xdr_int(xdrs, &objp->ndata))
- return (FALSE);
- if (objp->flags.fixed || xdrs->x_op == XDR_FREE)
- {
- if (!xdr_floatarray(xdrs, (char **)&objp->data,
- (u_int *)&objp->binAlloc, UINT_MAX))
- return (FALSE);
- if (!xdr_floatarray(xdrs, (char **)&objp->variance,
- (u_int *)&objp->binAlloc, UINT_MAX))
- return (FALSE);
- }
- else if (xdrs->x_op == XDR_DECODE)
- {
- objp->ndata = 0;
- objp->data = NULL;
- objp->variance = NULL;
- objp->binAlloc = 0;
- }
-
- if (fileVersion < 3.8001) /* add fuzz for float compare */
- {
- if (!xdr_vector(xdrs, (char *)oldTot,
- 9, sizeof(int), xdr_int))
- return (FALSE);
- for (i=0; i<3; i++)
- for (j=0; j<3; j++)
- objp->totals[i][j] = oldTot[i][j];
- }
- else
- {
- if (!xdr_vector(xdrs, (char *)objp->totals,
- 9, sizeof(int), xdr_float))
- return (FALSE);
- }
-
- if (!xdr_float(xdrs, &objp->binMin))
- return (FALSE);
- if (!xdr_float(xdrs, &objp->binMax))
- return (FALSE);
-
- return (TRUE);
- }
-
-
- bool_t xdr_axis_t(XDR *xdrs, axis_t *objp)
- {
- void *p;
- unsigned int c;
-
- if (!xdr_float(xdrs, &objp->low)) {
- return (FALSE);
- }
- if (!xdr_float(xdrs, &objp->high)) {
- return (FALSE);
- }
- if (!xdr_mywrapstring(xdrs, (char **)&objp->label )) {
- return (FALSE);
- }
-
- p = (void *) &objp->flags;
- c = sizeof(objp->flags);
- if (!xdr_bytes(xdrs, &p, &c, c))
- {
- return (FALSE);
- }
-
- if (!xdr_float(xdrs, &objp->tickLength)) {
- return (FALSE);
- }
- if (!xdr_float(xdrs, &objp->scaleFontSize)) {
- return (FALSE);
- }
- if (!xdr_float(xdrs, &objp->firstTick)) {
- return (FALSE);
- }
- if (!xdr_float(xdrs, &objp->tickStep)) {
- return (FALSE);
- }
- if (!xdr_int(xdrs, &objp->numMinorTicks)) {
- return (FALSE);
- }
-
- return (TRUE);
- }
-
-
- bool_t xdr_bind_strt_t(XDR *xdrs, bind_strt_t *objp)
- {
- if (!xdr_int(xdrs, &objp->x)) {
- return (FALSE);
- }
- if (!xdr_int(xdrs, &objp->y)) {
- return (FALSE);
- }
- if (!xdr_int(xdrs, &objp->z)) {
- return (FALSE);
- }
- if (!xdr_int(xdrs, &objp->weight)) {
- return (FALSE);
- }
- if (!xdr_int(xdrs, &objp->xerror)) {
- return (FALSE);
- }
- if (!xdr_int(xdrs, &objp->yerror)) {
- return (FALSE);
- }
- return (TRUE);
- }
-
-
- bool_t xdr_display_t(XDR *xdrs, display_t *objp)
- {
- void *p;
- unsigned int c;
-
- /*
- * write the ntuple pointer as an integer. Integer is index into
- * the list of ntuple in a record.
- */
- if (!xdr_int(xdrs, (int *)&objp->ntuple)) {
- return (FALSE);
- }
- if (!xdr_int(xdrs, &objp->nt_rev)) {
- return (FALSE);
- }
- if (!xdr_mywrapstring(xdrs, (char **)&objp->ntFile ))
- {
- return (FALSE);
- }
-
- if (!xdr_int(xdrs, &objp->dim)) {
- return (FALSE);
- }
- if (!xdr_enum(xdrs, (int *)&objp->graphtype)) {
- return (FALSE);
- }
- if (!xdr_enum(xdrs, (int *)&objp->drawtype)) {
- return (FALSE);
- }
-
- p = (void *) &objp->flags;
- c = sizeof(objp->flags);
- if (!xdr_bytes(xdrs, &p, &c, c))
- {
- return (FALSE);
- }
-
- if (!xdr_mywrapstring(xdrs, (char **)&objp->title ))
- {
- return (FALSE);
- }
-
-
- if (!xdr_bins_t(xdrs, &objp->bins))
- {
- return (FALSE);
- }
-
- if (!xdr_axis_t(xdrs, &objp->xAxis)) {
- return (FALSE);
- }
- if (!xdr_axis_t(xdrs, &objp->yAxis)) {
- return (FALSE);
- }
- if (!xdr_axis_t(xdrs, &objp->zAxis)) {
- return (FALSE);
- }
-
- if (!xdr_enum(xdrs, (int *)&objp->plotSymbol)) {
- return (FALSE);
- }
- if (!xdr_float(xdrs, &objp->symbolSize)) {
- return (FALSE);
- }
- if (!xdr_enum(xdrs, (int *)&objp->lineStyle)) {
- return (FALSE);
- }
- if (!xdr_bind_strt_t(xdrs, &objp->binding)) {
- return (FALSE);
- }
- if (!xdr_rectangle(xdrs, &objp->drawRect)) {
- return (FALSE);
- }
- if (!xdr_rectangle(xdrs, &objp->marginRect)) {
- return (FALSE);
- }
- if (!xdr_func_id(xdrs, &objp->nt_cut)) {
- return (FALSE);
- }
- if (!xdr_func_id(xdrs, &objp->bin_func)) {
- return (FALSE);
- }
- if (!xdr_func_id(xdrs, &objp->plot_func)) {
- return (FALSE);
- }
- if (!xdr_func_id(xdrs, &objp->binToColor)) {
- return (FALSE);
- }
-
- return (TRUE);
- }
-
-
-
-
- bool_t xdr_display(XDR *xdrs, display *objp)
- {
- if (!xdr_pointer(xdrs, (char **)objp, sizeof(display_t),
- xdr_display_t)) {
- return (FALSE);
- }
- return (TRUE);
- }
-
-
-
- bool_t xdr_hippo_rec(XDR *xdrs, hippo_rec *objp)
- {
- char *str = STRUCT_VERSION;
- int l,i;
- char msg[80];
-
- /*
- * first check that the magic number and structure version
- * are the same as I was compiled with.
- */
- if (strcmp(objp->magic, MAGIC) != 0)
- return (FALSE);
- if (strcmp(objp->s_version, STRUCT_VERSION) != 0)
- return (FALSE);
-
- /*
- * read/write the magic number. Pad (with NULL) or truncate to 4 bytes.
- */
- strncpy(msg,MAGIC,4);
- l = strlen(MAGIC);
- if (l>4) l=4;
- switch (xdrs->x_op)
- {
- case XDR_FREE:
- free(objp->magic);
- break;
-
- case XDR_DECODE: /* ie. read */
- if (! XDR_GETBYTES(xdrs,msg,4*sizeof(char)))
- return FALSE;
- break;
-
- case XDR_ENCODE:
- if (! XDR_PUTBYTES(xdrs,msg,4*sizeof(char)))
- return FALSE;
- break;
- }
- if (xdrs->x_op == XDR_DECODE && strncmp(msg,MAGIC,l) != 0)
- return (FALSE);
-
- /*
- * read/write the structure version.
- *
- */
- if (xdrs->x_op != XDR_DECODE)
- str = objp->s_version;
- else
- str = NULL;
- if (!xdr_mywrapstring(xdrs, (char **)&str ))
- return FALSE;
-
- /*
- * check the version number.
- */
- if (xdrs->x_op == XDR_DECODE)
- {
- i = sscanf(str,"%*s %*s %f",&fileVersion);
- if (i < 1 || fileVersion < 3.5)
- {
- sprintf(msg,"Old hippo file version: %s",str);
- h_error(msg);
- h_error("No displays will be read");
- }
- free(str);
- }
- else
- fileVersion = CURRENT_VER_NUM;
-
-
- if (!xdr_array(xdrs, (char **) &objp->nt_list,
- (u_int *)&objp->num_nt, UINT_MAX,
- sizeof(ntuple), xdr_ntuple ))
- {
- return (FALSE);
- }
-
- if (fileVersion < 3.5001) /* add fuzz for float compare */
- objp->num_disp = 0;
- else
- if (!xdr_array(xdrs, (char **)&objp->disp_list,
- (u_int *)&objp->num_disp, UINT_MAX,
- sizeof(display), xdr_display ))
- {
- return (FALSE);
- }
-
- return (TRUE);
- }
-